Python API Reference
This chapter provides information on eCognition's python API with reference for each class, properties, and methods and gives hints on printing and the usage of external python files in inline scripts. Exemplary python scripts can be found in User Guide >
How to set up and use an external debugger for python files see User Guide > Python Integration > Debugging python code in eCognition Developer
For more information see also:
Installation Guide > Windows > Python Installation - installation and setup
Reference Book > Algorithms and Processes > Miscellaneous > Python Script - description algorithm and its parameters
User Guide > Python Integration - application examples for python scripts and Debugging python code in eCognition Developer
Module eCognition
eCognition's algorithm 'python script' calls a specified python function with a single function argument of type dict, that contains all of the selected algorithm parameters.
Function argument structure:
{
"scene_info": {
"LLX": float,
"LLY": float
"resolution": float,
"projection_string": str,
"unit": str
},
"raster": {
"name1": ecognition.Raster,
"name2": ecognition.Raster,
...
},
"vector": {
"name1": ecognition.Vector,
"name2": ecognition.Vector,
...
},
"point_cloud": {
"name1": ecognition.PointCloud,
"name2": ecognition.PointCloud,
...
},
"variable" : {
"name1": str | float,
"name2": str | float,
...
},
"region" : {
"name1": numpy.ndarray (check set_region function for region explanation),
"name2": numpy.ndarray (check set_region function for region explanation),
...
},
"array" : {
"name1": list[str | float],
"name2": list[str | float],
...
}
}
Here “name*” will be equal to the name of the selected variable or layer in the “execute python script” process dialog.
class Raster
Available methods:
read_data(x=0, y=0, sx=0, sy=0)
Reads data from the raster image object. Can be called multiple times, to allow reading data in chunks for large rasters.
write_data(data, x_offset=0, y_offset=0)
Writes data to the raster image object. Can be called multiple times, to allow writing data in chunks for large rasters.
size()
Returns the size of the raster image.
get_datatype()
Returns the data type of the raster image.
close()
Finalizes raster object. Must be called for new raster objects created with ecognition.Raster.create_raster method.
Available static methods:
create_raster(name, type)
Static method
Creates an empty raster with the given name and type. If the raster with the specified name already exists, it is deleted, before a new one is created.
create_raster(name, data)
Static method
Creates a raster from the given data, with the given name. Data type is the same as data.dtype. If the raster with the specified name already exists, it is deleted, before a new one is created.
class Vector
Available methods:
get_geometry_type()
Returns the type of the vector geometry.
get_data()
Returns data of the vector as a pandas DataFrame. Geometries are located in the “geometry” column. Other attributes are contained in other columns. Geometries are objects from the shapely.geometry module.
close()
Finalizes the vector object. Must be called for new vector objects created with ecognition.Vector.create_vector method.
Available static methods:
create_vector(name, geometry_type, data)
Static method
Creates a vector from the given data, with the given name. If the vector with the specified name already exists, it is deleted, before a new one is created. pandas DataFrame given in the data argument must contain a “geometry” column, with at least one geometry. The elements of each DataFrame column must have the same type.
class PointCloudChunk
Available attributes:
x
Read-only property
Returns a numpy array of x coordinate values.
y
Read-only property
Returns a numpy array of y coordinate values.
z
Read-only property
Returns a numpy array of z coordinate values.
intensity
Read-only property
Returns a numpy array of intensity values.
red
Read-only property
Returns a numpy array of red values.
green
Read-only property
Returns a numpy array of green values.
blue
Read-only property
Returns a numpy array of blue values.
classification
Read-only property
Returns a numpy array of classification values.
Available special methods:
__len__()
Returns the number of points in the chunk.
__repr__()
Returns the string representation of the chunk.
class PointCloudChunkIterator
Available special methods:
__iter__()
Returns self.
__next__()
Returns the next point cloud chunk
class PointCloud
Available methods:
get_chunk_iterator(points_in_chunk=1000000)
Creates a chunk iterator, capable of iterating over the point cloud points. Only one PointCloudChunkIterator may exist at one time.
add_points(x, y, z, intensity, red, green, blue, classification)
Adds point cloud points to the PointCloud object. Each field array must have the same size. Values of the same index correspond to the same point.
get_point_count()
Returns the number of points in the PointCloud.
close()
Finalizes PointCloud object. Must be called for new PointCloud objects created with ecognition.PointCloud.create_point_cloud method.
Available static methods
create_point_cloud(name)
Static method
Creates an empty point cloud, with the given name. If the point cloud with the specified name already exists, it is deleted, before a new one is created.
Functions
set_variable(name, value)
Sets an eCognition variable. If it does not exist, it is created.
set_array(name, values)
Fills an eCognition array with the given values. If an array does not exist, it is created. All elements of the values sequence must have the same type.
set_region(name, region)
Sets an eCognition region. If a region does not exist, it is created. The region array must have a specific shape:
x_pos, y_pos, z_pos, and t_pos are the starting point of a region.
x_size, y_size, z_size, and t_size are the extent of a region.
wait_for_debugger(host=”localhost”, port=5678)
Blocks code execution and waits for a debugger to connect. Listens on a specified host and port. Debugger is implemented using a debugpy package: “https://github.com/microsoft/debugpy”, so it is possible to attach to the eCognition process and debug python code using tools that support debugpy.
Please refer to User Guide > Python Integration >Debugging python code in eCognition Developer for details.
write_message(message, show_message_box=False)
Writes a message to the eCognition log file. It also shows up in the View -> Message Console window. If show_message_box is True, then in addition, an information window box shows up with the message. Script execution is blocked until the message box is closed.
Hints
Hint 1 - Printing from python script
Because the console window is disabled in eCognition Developer, Pythons standard output to stdout is not visible. This hint lists some alternatives, in case you need to print some debug information during the development of your script.
The simplest way to print a message from python is to use a ecognition.write_message function:
import ecognition as ecog
def print_message(input_dict):
print_string = "Use write_message function"
ecog.write_message(print_string, True)
Another way to print debug messages would be to use an external log file (a dedicated logging package is recommended for this approach). For example:
Additionally you can log all contents to an external file. For example:
Hint 2 - Using external python files in inline script
If you need to import an external file into a python script, the script must be enabled to find that external file. That is done by adding the location of the external file into the process parameter “External script paths”. For example, for the following script:
C:\scripts\ExternalFile.py
And this inline python script:
Important - the parameter “External scripts path” must be defined as an array that contains “C:\scripts” as string value.